package ca.ubc.cs304.library.gui.helper;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

//import com.sun.codemodel.internal.fmt.JTextFile;

import ca.ubc.cs304.library.gui.LibWindow;


public class LibView {
	
	private static final int MAX_CPNT = 8;
	// Borrower Fields
	private static final String SEARCH_BOOK [] = {"Title", "Main Author", "Subject"};
	private static final String CHECK_ACCOUNT [] = {"SIN or Student Number", "Password"};
	private static final String PLACE_HOLD [] = {"SIN or Student Number", "Call Number", "Password"};
	private static final String PAY_FINE [] = {"SIN or Student Number", "Password", "Fine ID"};
	
	// Librarian Fields	
	private static final String ADD_BOOK [] = {"ISBN", "Title", "Main Author", "Publisher", "Year", "Subject"};	
	private static final String REMOVE_BOOK [] = {"Call Number", "Copy Number"};	
	private static final String REMOVE_BORROWER [] = {"Borrower ID"};	
	private static final String GENERATE_REPORT_1 [] = {"Subject"};	
	private static final String GENERATE_REPORT_2 [] = {"Year", "Max Return"};

	// Clerk Fields	
	private static final String CHECK_OVERDUE [] = {""};	
	
	public static void clearText(JTextField [] text) {
		for(int i = 0 ; i < MAX_CPNT ; i++) {
			text[i].setText("");
		}
	}
	
	// -----------------------------------------------------------------------------------------------------------------------------
	// Borrower Actions
	// -----------------------------------------------------------------------------------------------------------------------------
	public static void searchBook (JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < labl.length; i++) {
			if (i < SEARCH_BOOK.length) {
				labl[i].setText(SEARCH_BOOK[i]);
				labl[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
			}
		}	
		
		for(int i = 0 ; i < text.length; i++) {
			if (i < SEARCH_BOOK.length - 1) {
				text[i].setVisible(true);
			} else {
				text[i].setVisible(false);
			}
		}		
		
	}
	
	public static void checkAccount (JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < text.length || i < labl.length; i++) {
			if (i < CHECK_ACCOUNT.length) {
				labl[i].setText(CHECK_ACCOUNT[i]);
				labl[i].setVisible(true);
				text[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
				text[i].setVisible(false);
			}
		}		
	}
	
	public static void placeHold (JLabel [] labl, JTextField [] text) {
		clearText(text);	

		for(int i = 0 ; i < text.length || i < labl.length; i++) {
			if (i < PLACE_HOLD.length) {
				labl[i].setText(PLACE_HOLD[i]);
				labl[i].setVisible(true);
				text[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
				text[i].setVisible(false);
			}
		}		
	}
	
	public static void payFine (JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < text.length || i < labl.length; i++) {
			if (i < PAY_FINE.length) {
				labl[i].setText(PAY_FINE[i]);
				labl[i].setVisible(true);
				text[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
				text[i].setVisible(false);
			}
		}		
	}

	// -----------------------------------------------------------------------------------------------------------------------------
	// Librarian Actions
	// -----------------------------------------------------------------------------------------------------------------------------
	public static void addBook(JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < labl.length; i++) {
			if (i < ADD_BOOK.length) {
				labl[i].setText(ADD_BOOK[i]);
				labl[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
			}
		}
		
		for(int i = 0 ; i < text.length; i++) {
			if (i < ADD_BOOK.length - 1) {
				text[i].setVisible(true);
			} else {
				text[i].setVisible(false);
			}
		}		
	}

	public static void removeBook (JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < text.length || i < labl.length; i++) {
			if (i < REMOVE_BOOK.length) {
				labl[i].setText(REMOVE_BOOK[i]);
				labl[i].setVisible(true);
				text[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
				text[i].setVisible(false);
			}
		}
	}
	
	
	public static void removeBorrower (JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < text.length || i < labl.length; i++) {
			if (i < REMOVE_BORROWER.length) {
				labl[i].setText(REMOVE_BORROWER[i]);
				labl[i].setVisible(true);
				text[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
				text[i].setVisible(false);
			}
		}	
	}
	
	public static void generateReport1 (JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < labl.length; i++) {
			if (i < GENERATE_REPORT_1.length) {
				labl[i].setText(GENERATE_REPORT_1[i]);
				labl[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
			}
		}		
	
		for(int i = 0 ; i < text.length; i++) {
			if (i < GENERATE_REPORT_1.length - 1) {
				text[i].setVisible(true);
			} else {
				text[i].setVisible(false);
			}
		}			
	}
	
	public static void generateReport2 (JLabel [] labl, JTextField [] text) {
		clearText(text);
		
		for(int i = 0 ; i < text.length || i < labl.length; i++) {
			if (i < GENERATE_REPORT_2.length) {
				labl[i].setText(GENERATE_REPORT_2[i]);
				labl[i].setVisible(true);
				text[i].setVisible(true);
			} else {
				labl[i].setVisible(false);
				text[i].setVisible(false);
			}
		}		
	}
	
	
	// -----------------------------------------------------------------------------------------------------------------------------
	// Clerk Actions
	// -----------------------------------------------------------------------------------------------------------------------------
	public static void createClerkAddBorrowerLayout(JPanel panelToDisplayedIn, JTextField sinOrStudentNumberTextField, JTextField nameTextField, JPasswordField passwordTextField, JTextField addressTextField, JTextField phoneNumberTextField, JTextField emailAddressTextField, JTextField expiryDateTextField, JComboBox userTypeComboBox) {
		panelToDisplayedIn.removeAll();
		panelToDisplayedIn.repaint();
		
		JLabel sinOrStudentNumberLabel = new JLabel("SIN or Student #");
		JLabel nameTextLabel = new JLabel("Name");
		JLabel passwordLabel = new JLabel("Password");
		JLabel addressLabel = new JLabel("Address");
		JLabel phoneNumberLabel = new JLabel("Phone Number");
		JLabel emailLabel = new JLabel("Email");
		JLabel expiryDateLabel = new JLabel("Expiry Date");
		JLabel userTypeLabel = new JLabel("UserType");
		
		sinOrStudentNumberTextField.setText("");
		nameTextField.setText("");
		passwordTextField.setText("");
		addressTextField.setText("");
		phoneNumberTextField.setText("");
		emailAddressTextField.setText("");
		expiryDateTextField.setText("");
		if (userTypeComboBox.getItemCount() > 0) {
			userTypeComboBox.setSelectedIndex(0);
		}
		
		panelToDisplayedIn.setLayout(new GridBagLayout());
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(sinOrStudentNumberLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(sinOrStudentNumberTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(nameTextLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(nameTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(passwordLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(passwordTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(addressLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(addressTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(phoneNumberLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(phoneNumberTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 5;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(emailLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 5;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(emailAddressTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 6;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(expiryDateLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 6;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(expiryDateTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 7;
		gridBagConstraints.anchor = GridBagConstraints.LINE_END;
		panelToDisplayedIn.add(userTypeLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 7;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(userTypeComboBox,gridBagConstraints);
		
		panelToDisplayedIn.validate();
		panelToDisplayedIn.setVisible(true);
	}

	public static void createClerkCheckOutItemLayout(JPanel panelToDisplayedIn, JTextField borrowerIdTextField, JTextField[] callNumberTextFields, JTextField[] copyNumberTextFields) {
		panelToDisplayedIn.removeAll();
		panelToDisplayedIn.repaint();
		
		JLabel borrowerIdLabel = new JLabel("Borrower ID");
		JLabel callNumberLabel = new JLabel("Call Number");
		JLabel copyNumberLabel = new JLabel("Copy Number");
		
		borrowerIdTextField.setText("");
		for (int i=0; i < callNumberTextFields.length; i++) {
			callNumberTextFields[i].setText("");
		}
		for (int i=0; i < copyNumberTextFields.length; i++) {
			copyNumberTextFields[i].setText("");
		}
		
		panelToDisplayedIn.setLayout(new GridBagLayout());
		
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 1;
		gridBagConstraints.ipady = 64;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(borrowerIdLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.ipady = 0;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(borrowerIdTextField,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 1;
		gridBagConstraints.ipady = 0;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(callNumberLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 1;
		gridBagConstraints.ipady = 0;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(copyNumberLabel,gridBagConstraints);
		
		for (int i = 0; i < callNumberTextFields.length; i++) {
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = i + 2;
			gridBagConstraints.gridwidth = 3;
			gridBagConstraints.ipady = 0;
			gridBagConstraints.anchor = GridBagConstraints.LINE_START;
			panelToDisplayedIn.add(callNumberTextFields[i],gridBagConstraints);
			
			gridBagConstraints.gridx = 3;
			gridBagConstraints.gridy = i + 2;
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.ipady = 0;
			gridBagConstraints.anchor = GridBagConstraints.LINE_START;
			panelToDisplayedIn.add(copyNumberTextFields[i],gridBagConstraints);
		}
		
		panelToDisplayedIn.validate();
		panelToDisplayedIn.setVisible(true);
	}
	
	public static void createClerkProcessReturnLayout(JPanel panelToDisplayedIn, JTextField[] callNumberTextFields, JTextField[] copyNumberTextFields) {
		panelToDisplayedIn.removeAll();
		panelToDisplayedIn.repaint();
		
		JLabel callNumberLabel = new JLabel("Call number");
		JLabel copyNumberLabel = new JLabel("Copy Number");
		
		callNumberTextFields[0].setText("");
		copyNumberTextFields[0].setText("");
		
		panelToDisplayedIn.setLayout(new GridBagLayout());
		
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 1;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(callNumberLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 1;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(copyNumberLabel,gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(callNumberTextFields[0],gridBagConstraints);
		
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 1;
		gridBagConstraints.anchor = GridBagConstraints.LINE_START;
		panelToDisplayedIn.add(copyNumberTextFields[0],gridBagConstraints);
		
		panelToDisplayedIn.validate();
		panelToDisplayedIn.setVisible(true);
	}

	public static void createClerkCheckOverdueLayout(JPanel panelToDisplayedIn) {
		panelToDisplayedIn.removeAll();
		panelToDisplayedIn.repaint();
		
		panelToDisplayedIn.setLayout(new GridBagLayout());
		
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		panelToDisplayedIn.add(new JLabel("Click on the execute button to see the result in the result tab."),gridBagConstraints);
		
		panelToDisplayedIn.validate();
		panelToDisplayedIn.setVisible(true);
	}
	
	public static void updateClerkCheckOverdueLayout(JPanel panelToDisplayedIn) {
		panelToDisplayedIn.removeAll();
		panelToDisplayedIn.repaint();
		
		panelToDisplayedIn.setLayout(new GridBagLayout());
		
		panelToDisplayedIn.setLayout(new BorderLayout());
		panelToDisplayedIn.add(LibWindow.getInstance().getClkScroll(),BorderLayout.CENTER);
		
		panelToDisplayedIn.validate();
		panelToDisplayedIn.setVisible(true);
	}
}
